/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-12-17     Wayne        The first version
 */
#include <lvgl.h>
#include <rtthread.h>
#include "lcd_port.h"
#include "nuc970_2d.h"
#include "mmu.h"
#include "drv_sys.h"

#define LOG_TAG             "lvgl.disp"
#define DBG_ENABLE
#define DBG_SECTION_NAME   LOG_TAG
#define DBG_LEVEL DBG_ERROR
#define DBG_COLOR
#include <rtdbg.h>

/*A static or global variable to store the buffers*/
static lv_disp_draw_buf_t disp_buf;
static lv_disp_drv_t disp_drv;  /*Descriptor of a display driver*/

//static rt_device_t lcd_device = 0;
//static struct rt_device_graphic_info info;

static void tft_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
{
#ifndef BSP_USING_GE2D
    rt_int32_t x, y;
    lv_coord_t hres = disp_drv->hor_res;
    lv_coord_t vres = disp_drv->ver_res;

    /*Return if the area is out the screen*/
    if(area->x2 < 0 || area->y2 < 0 || area->x1 > hres - 1 || area->y1 > vres - 1) {
        lv_disp_flush_ready(disp_drv);
        return;
    }

    for(y = area->y1; y <= area->y2 && y < disp_drv->ver_res; y++) {
        for(x = area->x1; x <= area->x2 && x < disp_drv->hor_res; x++) {
            ((UINT16*)u8FrameBufPtr)[y * disp_drv->hor_res + x] = lv_color_to16(*color_p);
            color_p++;
        }
    }
#else
	unsigned int src_width = lv_area_get_width(area);
	unsigned int src_height = lv_area_get_height(area);
	unsigned int dst_x = area->x1;
	unsigned int dst_y = area->y1;

//    int x, y;
//    for(y = area->y1; y <= area->y2 && y < disp_drv->ver_res; y++) {
//        for(x = area->x1; x <= area->x2 && x < disp_drv->hor_res; x++) {
//            rt_kprintf("%02x ", lv_color_to16(*color_p));
//            color_p++;
//        }
//        rt_kprintf("\n");
//    }rt_kprintf("\n");
#if defined(BSP_USING_MMU)
    mmu_clean_invalidated_dcache((rt_uint32_t)color_p, src_width * src_height * sizeof(lv_color_t));
#else
    sysFlushCache(I_D_CACHE);
#endif

    while((inpw(REG_GE2D_MISCTL) & 0x100) != 0);

    outpw(REG_GE2D_SDPITCH, ((disp_drv->hor_res << 16) | src_width));
    outpw(REG_GE2D_DSTSPA, ((dst_y << 16) | dst_x));
    outpw(REG_GE2D_RTGLSZ, ((src_height << 16) | src_width));

    outpw(REG_GE2D_TRG, 1);

    while((inpw(REG_GE2D_INTSTS) & 0x1) == 0);
    outpw(REG_GE2D_INTSTS, 1);
#endif

    lv_disp_flush_ready(disp_drv);
}

void lcd_perf_monitor(struct _lv_disp_drv_t *disp_drv, uint32_t time, uint32_t px)
{
    rt_kprintf("Elapsed: %dms, Pixel: %d, Bytes:%d\n", time, px, px * sizeof(lv_color_t));
}

void lv_port_disp_init(void)
{
//    rt_err_t result;

//    lcd_device = rt_device_find("lcd");
//    if (lcd_device == 0)
//    {
//        LOG_E("error!");
//        return;
//    }

//    /* get framebuffer address */
//    result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
//    if (result != RT_EOK && info.framebuffer == RT_NULL)
//    {
//        LOG_E("error!");
//        /* get device information failed */
//        return;
//    }

//    RT_ASSERT(info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
//              info.bits_per_pixel == 24 || info.bits_per_pixel == 32);

//    result = rt_device_open(lcd_device, 0);
//    if (result != RT_EOK)
//    {
//        LOG_E("error!");
//        return;
//    }

    void *draw_buf1 = RT_NULL;

    draw_buf1 = (void *)rt_malloc(LCD_WIDTH * 68 * sizeof(lv_color_t));
    rt_kprintf("LVGL: Use one buffers - buf1@%08x\n", draw_buf1);
#ifdef BSP_USING_GE2D
    unsigned int cmd32 = 0xCC410000;
    outpw(REG_GE2D_CTL, cmd32);

    switch(LV_COLOR_DEPTH) {
    case 8:
        cmd32 = 0x00;
    break;
    case 16:
        cmd32 = 0x10;
    break;
    case 32:
        cmd32 = 0x20;
    break;
	}
    cmd32 |= (inpw(REG_GE2D_MISCTL) & 0xFFFFFFF8);
    outpw(REG_GE2D_MISCTL, cmd32);
    outpw(REG_GE2D_SRCSPA, 0);
    outpw(REG_GE2D_XYSORG, (unsigned int)draw_buf1);
    outpw(REG_GE2D_XYDORG, (unsigned int)LCDFrameBufPtr);
#endif
    /*Initialize `disp_buf` with the buffer(s).*/
    lv_disp_draw_buf_init(&disp_buf, draw_buf1, RT_NULL, LCD_WIDTH * 68);

    lv_disp_drv_init(&disp_drv); /*Basic initialization*/

    /*Set the resolution of the display*/
    disp_drv.hor_res = LCD_WIDTH;
    disp_drv.ver_res = LCD_HEIGHT;

//    disp_drv.full_refresh = 1;

    /*Set a display buffer*/
    disp_drv.draw_buf = &disp_buf;

    /*Write the internal buffer (draw_buf) to the display*/
    disp_drv.flush_cb = tft_flush;

    /* Called after every refresh cycle to tell the rendering and flushing time + the number of flushed pixels */
    //disp_drv.monitor_cb = lcd_perf_monitor;

    /*Finally register the driver*/
    lv_disp_drv_register(&disp_drv);
}
